#include <stdlib.h>
#include <stdio.h>
#include <math.h>
/* my version---don't do the log version...simply bin with #npart in some increments */

int main(int argc, char *argv[])
{
  int i, j, nbin=0, *na;
  int minpart, maxpart, inc;
  double m, x[3];
  char string[1000];
  FILE* fp1;

  if (argc != 6) {
    fprintf(stderr,"Usage: binmass Mmin Mmax inc inputfile outputfile\n");
    exit(1);
  }
  minpart = atoi(argv[1]);
  maxpart = atoi(argv[2]);
  inc = atoi(argv[3]);
  nbin = floor((maxpart-minpart)/inc);

  na = calloc(nbin, sizeof(int));
  fp1=fopen(argv[4], "r");
  while (!feof(fp1)) if (fgets(string, 999, fp1))
    if (sscanf(string, "%*d %lf %*d %lf %lf %lf",&m,x,x+1,x+2)==4) {
      
      i = floor((m-minpart)/inc);
      if (i>=0 && i<nbin) {++(na[i]);}
    }
  fclose(fp1);
  fp1=fopen(argv[5], "w");
  for (i=0; i<nbin; i++) {
    fprintf(fp1,"%d %d\n", minpart+i*inc, na[i]);
  }
  fclose(fp1);
  return 0;
}
